package com.wenhai.redis.lettuce;

import io.lettuce.core.RedisClient;
import io.lettuce.core.RedisFuture;
import io.lettuce.core.RedisURI;
import io.lettuce.core.SetArgs;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.async.RedisAsyncCommands;

import java.time.Duration;
import java.time.temporal.ChronoUnit;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

public class LettuceAsyncDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        RedisURI redisURI = RedisURI.builder()
                .withPassword("admin")
                .withHost("192.168.152.138")
                .withPort(6379)
                .withTimeout(Duration.of(10,ChronoUnit.SECONDS))
                .build();
        RedisClient redisClient = RedisClient.create(redisURI);
        StatefulRedisConnection<String, String> connect = redisClient.connect();
        RedisAsyncCommands<String, String> redisCommands = connect.async();
        SetArgs setArgs = SetArgs.Builder.nx().ex(5);
        RedisFuture<String> result = redisCommands.set("name", "wenhai", setArgs);
        result.thenAccept(System.out::println);
        System.out.println(result.get());
        TimeUnit.SECONDS.sleep(10);
        connect.close();
        redisClient.shutdown();

    }
}
